package com.sqx.modules.goodsType.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.goodsType.dao.GoodsDegreeDao;
import com.sqx.modules.goodsType.entity.GoodsDegree;
import com.sqx.modules.goodsType.service.GoodsDegreeService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Admin
* @description 针对表【goods_degree(商品新旧程度表)】的数据库操作Service实现
* @createDate 2024-10-14 09:39:25
*/
@Service
public class GoodsDegreeServiceImpl extends ServiceImpl<GoodsDegreeDao, GoodsDegree>
    implements GoodsDegreeService {

    @Override
    public Result addGoodsDegree(GoodsDegree goodsDegree) {
        if (goodsDegree.getId() != null){
            baseMapper.updateById(goodsDegree);
            return Result.success();
        }else {
            goodsDegree.setCreateTime(new Date());
            baseMapper.insert(goodsDegree);
            return Result.success();
        }
    }

    @Override
    public Result delGoodsDegree(Long id) {
        GoodsDegree goodsDegree = baseMapper.selectById(id);
        if (goodsDegree != null){
            baseMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("该数据不存在");
    }

    @Override
    public IPage<GoodsDegree> getGoodsDegreeList(Integer page, Integer limit, GoodsDegree goodsDegree) {
        Page<GoodsDegree> pages;
        if (page != null && limit != null){
            pages = new Page<>(page, limit);
        }else{
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.selectPage(pages,new QueryWrapper<>(goodsDegree));
    }

    @Override
    public Result getGoodsDegreeById(Long id) {
        GoodsDegree goodsDegree = baseMapper.selectById(id);
        if (goodsDegree != null){
            return Result.success().put("data",goodsDegree);
        }
        return Result.error("该数据不存在");
    }
}




